<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户注册页面</title>
    <link rel="stylesheet" th:href="@{/css/layui.css}">
    <script rel="script" th:src="@{/js/jquery-1.9.1.min.js}" charset="utf-8"></script>
</head>
<body>

<div class="container">
    <div class="layui-row">
        <div class="layui-col-md12"><h1 style="text-align: center;margin-top: 20px">邮箱注册登陆</h1></div>
    </div>
    <div style="text-align: center">
        <form class="layui-form layui-form-pane"  action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input style="width: 200px " type="email" id="email"  name="userEmail" required lay-verify="required"
                               placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input style="width: 150px" type="text" name="code" required lay-verify="required"
                               placeholder="请输入验证码" autocomplete="off" class="layui-input">
                    </div>
                    <button type="button" class="layui-btn"  id="sendCode" onclick="getCode()">获取验证码</button>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-block">
                    <input style="width: 200px" type="text" name="username" required  lay-verify="required" placeholder="请设置用户名" autocomplete="off" class="layui-input">
                </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input style="width: 200px" type="password" name="password" required lay-verify="required" placeholder="请设置密码"
                           autocomplete="off" class="layui-input">
                </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input style="width: 200px" type="text" name="name" required  lay-verify="required" placeholder="用户昵称" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" id="sub" lay-submit="" lay-filter="formDemo">注册</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

</div>
<script rel="script" th:src="@{/layui.js}"  charset="utf-8"></script>
<script th:src="@{/lib/jq-module/jquery.particleground.min.js}" charset="utf-8"></script>
<script th:inline="javascript">
    function getCode(){

        var email=document.getElementById("email");
        if(email.value=="")
        {
            alert("邮箱不能为空");
            return ;
        }
        var email_reg=/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if(!email_reg.test(email.value))
        {
            alert("请输入正确的邮箱地址");
            return ;
        }
        $.get("/getCode",{"email":email.value},function (response){
            if (response){
                var wait = 60;
                var button=document.getElementById("sendCode");
                (function time(o) {
                    if (wait == 0) {
                        o.disabled=false;
                        o.innerHTML = "获取验证码";
                        wait = 60;
                    } else {
                        o.disabled=true;
                        o.innerHTML = wait + "秒后可以重新发送";
                        wait--;
                        setTimeout(function() {
                            time(o)
                        }, 1000)
                    }
                })(button);
            }else {

            }


        });
    }
    layui.use(['jquery','form','layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        er =layui.layer;
        form.on('submit(formDemo)', function (data) {
            // 将注册按钮置灰不可用
            $("#sub").attr("disabled","disabled").addClass("layui-btn-disabled");
             console.log(data);
            data=data.field;
            if (data.username == '') {
                er.msg('用户名不能为空');
                return false;
            }
            if (data.password == '') {
                er.msg('密码不能为空');
                return false;
            }
            if (data.code == '') {
                er.msg('验证码不能为空');
                return false;
            }

            // 异步登录请求

            $.get('/zhuce',data,function (response) {

                if(response.code == 200){
                    layer.msg('注册成功', {icon: 6, time:1000},function () {
                        window.location = '/index';
                    });
                }else{
                    layer.msg(response.msg,{icon: 5,anim: 6});
                    // 将登录按钮取消置灰，恢复可用
                    $("#sub").removeAttr("disabled","disabled").removeClass("layui-btn-disabled");
                }
            });
            return false;
        });
    });
</script>


</body>
</html>